package com.example.rabbitmq;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.HashMap;
import java.util.Map;

@Component
public class Producer {

    @Resource
    RabbitTemplate rabbitTemplate;


    public void dispatcher(String email,String account){
        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("email",email);
        resultMap.put("account",account);
        try {
            ByteArrayOutputStream bao = new ByteArrayOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(bao);

            // 将对象写入到输出流中
            oos.writeObject(resultMap);

            // 将输出流转换为字节数组
            byte[] bytes = bao.toByteArray();
            rabbitTemplate.send("queue",new Message(bytes));
            // 关闭流
            oos.close();
            bao.close();
            new Consumer();
            // 现在 bytes 包含了序列化后的对象
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
